﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;

namespace _0912089
{
    public class PlaySettingScreen : ComponentView
    {
        bool isLeftClick = false;
        bool isPressEsc = false;
        private bool isBackMenuScreen = false;
        private bool isGoingToPlayScreen = false;
        List<GameBase.MapInfo> _listMapsInfo = new List<GameBase.MapInfo>();

        Game1 _game; 
        
        Button _back;
        Button _ok;
        Label _labelMapName;
        Label _labelMapSize;
        Label _labelNumberOfTeam;
        ImageView _backgroundMenu;
        ImageView _mapPreview;

        Label _labelMapNameValue;
        Label _labelMapSizeValue;
        Label _labelNumberOfTeamValue;

        Combobox _nextPreviousMap;

        ComponentView _panel;

        ComponentView _nightPanel;
        ImageView _loadingImage;
        ImageView _logoImage;

        public PlaySettingScreen(Game1 game)
        {            
            _game = game;

            loadAllMapsInfo();

            Size = new Vector2(GameBase.Constant.screenWidth, GameBase.Constant.screenHeight);
            _backgroundMenu = new ImageView(@"Resources/background6", new Vector2(0, 0), 1);

            Vector2 sizePanel = new Vector2(GameBase.Constant.screenWidth - 200, GameBase.Constant.screenHeight - 200);
            Vector2 sizeGameScreen = new Vector2(GameBase.Constant.screenWidth, GameBase.Constant.screenHeight);

            _panel = new ComponentView(@"Resources/panel", new Vector2(100, -600), sizePanel);
            _panel.Alpha = 0.4f;
            _back = new Button(@"Resources/back", new Vector2(600, 430), 0.5f);
            _ok = new Button(@"Resources/ok", new Vector2(850, 430), 0.5f);

            _labelMapName = new Label(@"Resources/fontNormal", @"Map name", new Vector2(600, 80), Color.Blue, 0.75f);
            _labelMapSize = new Label(@"Resources/fontNormal", @"Map size", new Vector2(600, 160), Color.Blue, 0.75f);
            _labelNumberOfTeam = new Label(@"Resources/fontNormal", @"Number of team", new Vector2(600, 240), Color.Blue, 0.75f);
            _mapPreview = new ImageView(_listMapsInfo[0].Background, new Vector2(100, 10), new Vector2(400,400));

            _labelMapNameValue = new Label(@"Resources/fontNormal", _listMapsInfo[0].Name, new Vector2(850, 80), Color.Red, 0.75f);
            _labelMapSizeValue = new Label(@"Resources/fontNormal", _listMapsInfo[0].Size, new Vector2(850, 160), Color.Red, 0.75f);
            _labelNumberOfTeamValue = new Label(@"Resources/fontNormal", _listMapsInfo[0].Positions.Count.ToString(), new Vector2(850, 240), Color.Red, 0.75f);

            List<string> listNextPreviousMap = new List<string>();
            for (int i = 0; i < _listMapsInfo.Count; i++)
                listNextPreviousMap.Add((i + 1).ToString() + " / " + _listMapsInfo.Count.ToString());
            _nextPreviousMap = new Combobox(listNextPreviousMap, new Vector2(100, 440), 400, Color.Red, 0.75f);

            _panel.Action = new MoveAction(@"panelActionDone", _panel, (sizeGameScreen - sizePanel) / 2, 10.0f, 4.0f);
            _panel.Action.start();

            this.addToSubView(_backgroundMenu);
            this.addToSubView(_panel);
            _panel.addToSubView(_back);
            _panel.addToSubView(_ok);
            _panel.addToSubView(_labelMapName);
            _panel.addToSubView(_labelNumberOfTeam);
            _panel.addToSubView(_labelMapSize);
            _panel.addToSubView(_mapPreview);
            _panel.addToSubView(_labelMapNameValue);
            _panel.addToSubView(_labelMapSizeValue);
            _panel.addToSubView(_labelNumberOfTeamValue);
            _panel.addToSubView(_nextPreviousMap);

            this.subscribe(_panel.Action);

            initNightPanel();
        }

        private void initNightPanel()
        {
            _nightPanel = new ComponentView(@"Resources/black_background", new Vector2(0, 0), new Vector2(GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width, GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height));
            _nightPanel.Alpha = 0.7f;
            _nightPanel.Visible = false;

            _loadingImage = new ImageView(@"Resources/loadingImage", Vector2.Zero, 1);
            _loadingImage.Position = (new Vector2(GameBase.Constant.screenWidth, GameBase.Constant.screenHeight) - _loadingImage.Size) / 2;
            _logoImage = new ImageView(@"Resources/logo", Vector2.Zero, 1);
            _logoImage.Position = (new Vector2(GameBase.Constant.screenWidth, GameBase.Constant.screenHeight-200) - _logoImage.Size) / 2;

            _nightPanel.addToSubView(_logoImage);
            _nightPanel.addToSubView(_loadingImage);
            this.addToSubView(_nightPanel);
        }

        private void loadAllMapsInfo()
        {
            string startupPath = System.IO.Directory.GetCurrentDirectory();
            string[] fileNames = System.IO.Directory.GetFiles(startupPath + @"\Maps", "*.xml");
            foreach (string fileName in fileNames)
            {
                try
                {
                    GameBase.MapInfo mapInfo = new GameBase.MapInfo(fileName);
                    _listMapsInfo.Add(mapInfo);
                }
                catch
                {
                }
            }
        }

        public override void update(GameTime gameTime)
        { 
            base.update(gameTime);

            #region Mouse processing
            MouseState ms = Mouse.GetState();
            Vector2 targetPos = new Vector2(ms.X, ms.Y);

            if (ms.LeftButton == ButtonState.Pressed)
                isLeftClick = true;
            if (ms.LeftButton == ButtonState.Released && isLeftClick)
            {
                isLeftClick = false;
                if (_back.isContainPoint(targetPos))
                {
                    isBackMenuScreen = true;
                    reverseAllAction();
                }
                else if (_ok.isContainPoint(targetPos))
                {
                    isGoingToPlayScreen = true;
                    reverseAllAction();
                }
                else if (_nextPreviousMap.Next.isContainPoint(targetPos) || _nextPreviousMap.Previous.isContainPoint(targetPos))
                {
                    updateLayoutWithMap(_listMapsInfo[_nextPreviousMap.SelectedIndex]);
                }
            }
            #endregion

            #region KeyBoard processing
            KeyboardState kbs = Keyboard.GetState();
            if (kbs.IsKeyDown(Keys.Escape))
                isPressEsc = true;
            if (kbs.IsKeyUp(Keys.Escape) && isPressEsc && !isGoingToPlayScreen)
            {
                isPressEsc = false;
                isBackMenuScreen = true;
                reverseAllAction();
            }
            #endregion
        }

        private void updateLayoutWithMap(GameBase.MapInfo mapInfo)
        {
            _labelMapNameValue.Text = mapInfo.Name;
            _labelMapSizeValue.Text = mapInfo.Size;
            _labelNumberOfTeamValue.Text = mapInfo.Positions.Count.ToString();
            _mapPreview.Background = mapInfo.Background;
        }

        private void reverseAllAction()
        {
            _panel.Action.reverse();
            _panel.Action.start();
        }

        public void playAnimation()
        {
            reverseAllAction();
        }

        #region ISubscriber
        public override bool notify(string message)
        {
            base.notify(message);

            if (isBackMenuScreen)
            {
                Game1.currentGameScreen = Game1.GameScreen.Menu;
                _game.MenuScreen.playAnimation();
            }
            else if (isGoingToPlayScreen)
            {
                _nightPanel.Visible = true;
                Thread t = new Thread(new ThreadStart(updateView)); 
                t.Priority = ThreadPriority.Highest;
                t.Start(); 
            }
            isBackMenuScreen = false;
            return true;
        }
        #endregion

        void updateView()
        {
            _game.PlayingScreen = new PlayingScreen(_game, _listMapsInfo[_nextPreviousMap.SelectedIndex]);
            Game1.currentGameScreen = Game1.GameScreen.Playing;
            _nightPanel.Visible = false;
            isGoingToPlayScreen = false;
        }
    }
}
